package trocabi.view;

import trocabi.TROCABI;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import trocabi.model.Article;

public class JTableModelFacturationArticles extends AbstractTableModel {
    private final String[]     sEntetes = { "Référence", "Date de dépôt", "Situation", "Date de changement d'état", 
                                            "Prix d'achat", "Type", "Aspect", "Pour", "Couleur"};
    private ArrayList<Article> articles;

    public JTableModelFacturationArticles() {
        super();
        this.articles = new <Article>ArrayList();
    }

    public JTableModelFacturationArticles(ArrayList<Article> articles) {
        super();
        this.articles = articles;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return sEntetes[columnIndex];
    }

    @Override
    public int getColumnCount() {
        return sEntetes.length;
    }

    @Override
    public int getRowCount() {
        return articles.size();
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        if (getColumnName(col).equals("")) {
            return true;
        }
        return false;
    }

    @Override
    public Object getValueAt(int i, int i1) {
        switch (i1) {
            case 0:
                return articles.get(i).getId();
            case 1:
                return TROCABI.sdfFormat_Date.format(articles.get(i).getDateDepot());
            case 2:
                return articles.get(i).getSituation();
            case 3:
                return (articles.get(i).getDateChangementEtat() == null) ? "Pas de changement d'état" : TROCABI.sdfFormat_Date.format(articles.get(i).getDateChangementEtat());
            case 4:
                return TROCABI.dfFormat_Prix.format(Double.valueOf(articles.get(i).getPrixAchat())) + " €";
            case 5:
                return articles.get(i).getCodeType();
            case 6:
                return articles.get(i).getCodeAspect();
            case 7:
                return articles.get(i).getCodeGenre();
            case 8:
                return articles.get(i).getCouleur();
            default:
                return null;
        }
    }

    public Article getArticleAt(int row) {
        return articles.get(row);
    }

    public ArrayList<Article> getArticles() {
        return articles;
    }

    public void add(Article atArticle) {
        articles.add(atArticle);
        fireTableDataChanged();
    }

    public void add(ArrayList<Article> listeArticles) {
        articles = listeArticles;
        fireTableDataChanged();
    }

    public void remove(Article article) {
        articles.remove(article);
        fireTableDataChanged();
    }

    public void removeAll() {
        articles = new <Article>ArrayList();
        fireTableDataChanged();
    }

    public void refresh() {
        fireTableDataChanged();
    }
}
